﻿/*
 Copyright (c) 2011.  Dirk Gordon

 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the Software is
 furnished to do so, subject to the following conditions:

 The above copyright notice and this permission notice shall be included in
 all copies or substantial portions of the Software.

 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 THE SOFTWARE.
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using System.IO;

namespace File_CRC32_Verifier_WPF
{
    /// <summary>
    /// Interaction logic for CreateSFVWindow.xaml
    /// </summary>
    public partial class CreateSFVWindow : Window
    {
        private const string HEADER = ";     Created by File CRC32 Verifier on ";

        private ObservableCollection<FileInformationWithBoolean> fileCollectionWithBoolean = new ObservableCollection<FileInformationWithBoolean>();

        /// <summary>
        /// Creates a new CreateSFVWindow.
        /// </summary>
        /// <param name="mainWindowFileCollection">The file collection from the main window.</param>
        public CreateSFVWindow(ObservableCollection<FileInformation> mainWindowFileCollection)
        {
            InitializeComponent();

            if (mainWindowFileCollection == null)
                MessageBox.Show("Error: File Collection was null.");
            else
            {
                ObservableCollection<FileInformation> mainWindowFiles = mainWindowFileCollection;

                foreach (FileInformation file in mainWindowFiles)
                    fileCollectionWithBoolean.Add(new FileInformationWithBoolean(file));

                dataGrid.ItemsSource = fileCollectionWithBoolean;
            }
        }

        /// <summary>
        /// Creates the SFV file of all files that are checked in the dataGrid. Ignores files with blank CRC values.
        /// </summary>
        private void btnCreate_Click(object sender, RoutedEventArgs e)
        {
            //Get all checked files and order by fileName in descending order.
            var query = from f in fileCollectionWithBoolean
                        where f.isChecked && f.CRC != ""
                        orderby f.fileName descending
                        select f;

            if (query.Count() > 0)
            {
                //Open save dialog.
                Microsoft.Win32.SaveFileDialog saveFileDialog = new Microsoft.Win32.SaveFileDialog();
                saveFileDialog.Filter = "SFV files (*.sfv)|*.sfv";
                saveFileDialog.FilterIndex = 1;
                if (saveFileDialog.ShowDialog() == true)
                {
                    try
                    {
                        using (StreamWriter SFVFile = new StreamWriter(saveFileDialog.FileName))
                        {
                            //Header
                            SFVFile.WriteLine(HEADER + DateTime.Now.ToString("yyyy-MM-dd"));

                            //Line item
                            foreach (var result in query)
                            {
                                if (result.isChecked)
                                    SFVFile.WriteLine(System.IO.Path.GetFileName(result.fileName) + " " + result.CRC);
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.ToString());
                    }
                }
            }
        }

        /// <summary>
        /// Closes the window on button close click.
        /// </summary>
        private void btnClose_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }
    }



    /// <summary>
    /// The FileInformationWithBoolean class accepts a FileInformation file as input and stores a boolean value with it.
    /// </summary>
    public class FileInformationWithBoolean
    {
        private FileInformation _file;
        private bool _isChecked;
        
        /// <summary>
        /// Create new FileInformationWithBoolean object.
        /// </summary>
        /// <param name="file">The FileInformation file.</param>
        public FileInformationWithBoolean(FileInformation file)
        {
            _file = file;

            //Only set the checked state to true when the CRC has been calculated.
            if (file.actualCRC == "")
                _isChecked = false;
            else
                _isChecked = true;
        }

        /// <summary>
        /// Gets or sets whether the checkbox is checked or not.
        /// </summary>
        public bool isChecked
        {
            get
            {
                return _isChecked;
            }
            set
            {
                _isChecked = value;
            }
        }

        /// <summary>
        /// Returns the fileName.
        /// </summary>
        public string fileName
        {
            get
            {
                return _file.fileName;
            }
        }

        /// <summary>
        /// Returns the CRC.
        /// </summary>
        public string CRC
        {
            get
            {
                return _file.actualCRC;
            }
        }
    }
}
